package com.fsd.experiment8;

import android.Manifest;
import android.content.pm.PackageManager;
import android.media.MediaPlayer;
import android.os.PatternMatcher;
import android.provider.MediaStore;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.Editable;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import android.widget.VideoView;

import java.io.File;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_open_view);
        final EditText edt = findViewById(R.id.edittext);
        Button btn_open = findViewById(R.id.btn_open);
        final VideoView video = findViewById(R.id.video);
        Button btn_pause = findViewById(R.id.btn_pause);
        Button btn_reset = findViewById(R.id.btn_reset);

        btn_open.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String file_path = edt.getText().toString();
                File file = new File(file_path);
                if (!file.exists()) {
                    Toast.makeText(MainActivity.this, "视频文件路径错误", Toast.LENGTH_LONG).show();
                }
                else{
                    if(ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.WRITE_EXTERNAL_STORAGE)!= PackageManager.PERMISSION_GRANTED){
                        ActivityCompat.requestPermissions(MainActivity.this,new String[]{
                            Manifest.permission.WRITE_EXTERNAL_STORAGE},1);
                        }
                    else {

                        video.setVideoPath(file_path);
                        video.start();
                    }

                }
            }

        });
        btn_pause.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                video.pause();
            }
        });
        btn_reset.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                video.start();
            }
        });
    }
}
